package com.mywork.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.mywork.po.Curelist;
import com.mywork.po.Drug;
import com.mywork.po.Sign;
import com.mywork.service.CureListService;
import com.mywork.service.DrugService;
import com.mywork.service.MyJSONUtil;
import com.mywork.service.SignService;

@Controller
@RequestMapping("/ajax")
public class AJAXController {
	@Autowired
	CureListService cureService;
	
	@Autowired
	SignService SignService;
	
	@Autowired
	DrugService drugService;
	/**
	 * 根据药物名称或者助记码获取药品（）模糊查询
	 * @param var 参数名
	 * @param response
	 */
	@RequestMapping("/getDrugByDrugNameOrCode")
	public void getDrug(String var,HttpServletResponse response){
		PrintWriter out = MyJSONUtil.getJSONprintWriter(response);
		List<Drug> drug =drugService.getDrugByDrugNameOrMnemonicCode(var);
		out.write(JSON.toJSONString(drug).toString());
		out.flush();
		out.close();
	}
	
	
	/**
	 * Ajax通过上一级药品Id寻找药品
	 * @param lastDrugId 参数名
	 * @param response
	 * @param request
	 */
	@RequestMapping("/getDrugByLastDrugId")
	public void getDrugForAjax(String lastDrugId,HttpServletResponse response,HttpServletRequest request){
		//还没加验证
		
		List<Drug> drugs= drugService.getNextLevelDrugList(lastDrugId);
		PrintWriter out =MyJSONUtil.getJSONprintWriter(response);
		
		out.write(JSON.toJSON(drugs).toString());
		out.flush();
		out.close();
	}
	
	/**
	 * Ajax通过上一级治疗项目Id寻找治疗项目
	 * @param drugId
	 * @param response
	 */
	@RequestMapping("/getCrueByLastCrueId")
	public void forAjax(String lastCureId, HttpServletResponse response,HttpServletRequest request) {
		//还没加验证
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = MyJSONUtil.getJSONprintWriter(response);
		List<Curelist> cureListForLastCureId= cureService.getCureListByLastCureId(lastCureId);
//		
//		char[] json = JSON.toJSON(cureListForLastCureId).toString().toCharArray();
//		StringBuffer sb = new StringBuffer();
//		for(char s:json){
//			if(s=='"'){
//				sb.append('\\');
//				sb.append(s);
//			}else{
//				sb.append(s);
//			}
//		}
		out.print(JSON.toJSON(cureListForLastCureId).toString());
		out.flush();
		out.close();

	}

	/**
	 * 根据上一级的Id查出Sign
	 * @param lastSignId 参数
	 */
	@RequestMapping("/getSignByLastSignId")
	public void getSignByLastSignId(String lastSignId,HttpServletResponse response){
		
		List<Sign> signs=SignService.getSignForLastSignId(Integer.valueOf(lastSignId));
		PrintWriter out = MyJSONUtil.getJSONprintWriter(response);
		out.write(JSON.toJSONString(signs).toString());
		out.flush();
		out.close();
	}
}
